﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Text;
using System.Text.RegularExpressions;

namespace Reader.Utils;

public static class BookHelper
{
    public static List<ChapterContent> ParseChaptersWithContent(string filePath)
    {
        var chapters = new List<ChapterContent>();
        const string chapterPattern = @"^\s*第[零一二两三四五六七八九十百千万0-9零一二两三四五六七八九十百千万]+[章节部卷集回]\s*.*$";
        var currentChapter = new ChapterContent { Title = "序言", Content = string.Empty };
        chapters.Add(currentChapter);

        // 定义contentBuilder在using块之外
        StringBuilder contentBuilder = new StringBuilder();

        using (StreamReader reader = new StreamReader(filePath))
        {
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                if (Regex.IsMatch(line, chapterPattern, RegexOptions.IgnoreCase))
                {
                    currentChapter.Content = contentBuilder.ToString().TrimEnd('\r', '\n');
                    contentBuilder.Clear();
                    currentChapter = new ChapterContent { Title = line.Trim(), Content = string.Empty };
                    chapters.Add(currentChapter);
                }
                else
                {
                    contentBuilder.AppendLine(line);
                }
            }
        }

        currentChapter.Content = contentBuilder.ToString().TrimEnd('\r', '\n');

        return chapters;
    }
    
    private static Random random = new Random();

    public static void GenerateCover(string title, string author, string outputPath)
    {
        // 设置封面图片的大小
        int width = 600;
        int height = 800;

        // 创建一个位图对象
        using (Bitmap bitmap = new Bitmap(width, height))
        {
            // 获取Graphics对象，用于绘制图像
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                // 设置绘图质量
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
                graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

                // 生成亮色随机背景
                Color backgroundColor = GetBrightRandomColor();
                graphics.Clear(backgroundColor);

                // 设置字体和颜色
                Font titleFont = new Font("Arial", 40, FontStyle.Bold);
                Font authorFont = new Font("Arial", 20, FontStyle.Italic);

                // 计算文本位置
                SizeF titleSize = graphics.MeasureString(title, titleFont);
                SizeF authorSize = graphics.MeasureString(author, authorFont);

                float titleX = (width - titleSize.Width) / 2;
                float titleY = (height - titleSize.Height) / 2 - 50;
                float authorX = (width - authorSize.Width) / 2;
                float authorY = titleY + titleSize.Height + 20;

                // 绘制文本
                graphics.DrawString(title, titleFont, Brushes.Black, titleX, titleY);
                graphics.DrawString(author, authorFont, Brushes.Black, authorX, authorY);
            }

            // 保存图片
            bitmap.Save(outputPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }

    private static Color GetBrightRandomColor()
    {
        // 确保颜色是亮的
        int r = random.Next(128, 256);
        int g = random.Next(128, 256);
        int b = random.Next(128, 256);
        return Color.FromArgb(r, g, b);
    }
}